function [M]=CPopt_Demo(X,R)

%Create initial guess using 'nvecs'

M_init = create_guess('Data', X, 'Num_Factors', R, ...
    'Factor_Generator', 'nvecs');

%Set up the optimization parameters
% Get the defaults
ncg_opts = ncg('defaults');
% Tighten the stop tolerance (norm of gradient). This is often too large.
ncg_opts.StopTol = 1.0e-7;
% Tighten relative change in function value tolearnce. This is often too large.
ncg_opts.RelFuncTol = 1.0e-20;
% Increase the number of iterations.
ncg_opts.MaxIters = 3000;
% Only display every 10th iteration
ncg_opts.DisplayIters = 10;

%Call the cp_opt method
[M,~,~] = cp_opt(X, R, 'init', M_init, ...
    'alg', 'ncg', 'alg_options', ncg_opts);
end




